package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ysd.Dao.Impl.AdminDaoImpl;
import com.ysd.entity.Admin;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

/**
 * adminServlet?reqName=login
 * @author 86176
 *
 */
public class AdminServlet extends BaseServlet {
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String uName = request.getParameter("userName");
		String password = request.getParameter("password");
		AdminDaoImpl aDao = new AdminDaoImpl();
		
		Admin admin = aDao.getAdminByUserName(uName);
		
		if (admin == null) {//没查到
			out.print(Result.toClient(false, "","账号错误"));
		} else {//查到了,账号正确
			if (admin.getPassword().equals(password)) {//密码正确
				HttpSession session = request.getSession();//创建session对象
				session.setAttribute("admin", admin);//将admin存入服务器对象
				out.print(Result.toClient(true, admin,"登陆成功" ));
			} else {
				out.print(Result.toClient(false, "","密码错误"));
			}
		}
	}


}
